/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import datamodel.StringPair;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

/**
 *
 * @author Kvest
 */
public class MediaManager 
{
    public static final StringPair[] SOUNDS = new StringPair[]{
                                                                (new StringPair("tututu2.wav", "Tututu 2 wav")),
                                                                (new StringPair("alert_tone.mid", "Alert tone")),
                                                                (new StringPair("big_bin.mid", "Big bin")),
                                                                (new StringPair("barabany.mid", "Barabany")),
                                                                (new StringPair("budilnik.mid", "Budilnik")),
                                                                (new StringPair("capcom.mid", "Capcom")),
                                                                (new StringPair("down.wav", "Down wav")),
                                                                (new StringPair("galaxy.wav", "Galaxy wav")),
                                                                (new StringPair("icq.wav", "ICQ wav")),                                                               
                                                                (new StringPair("icq.mid", "ICQ mid")),
                                                                (new StringPair("drink_bar.mid", "Drink bar")),
                                                                (new StringPair("iles.mid", "Iles")),
                                                                (new StringPair("ingle.mid", "Ingle")),
                                                                (new StringPair("pacman.mid", "Pacman")),                                                                
                                                                (new StringPair("taxi_maxim.mid", "Taxi maxim")),
                                                                (new StringPair("tubularbells.mid", "Tubularbells")),
                                                                (new StringPair("tututu.wav", "Tututu wav")),
                                                                (new StringPair("uasterisk.wav", "UAsterisk wav")),
                                                                (new StringPair("uuup.wav", "Uuup wav"))};

    private static final String MEDIA_FOLDER = "/media/"; 
    
    private static final String TYPE_WAV = ".wav";
    private static final String TYPE_MID = ".mid";
    
    private static MediaManager manager = null;
    private static final Object lock = new Object();
    
    private Player player;
    private int currentMediaID;
    private InputStream is;

    private String mediaTypeWav;
    private String mediaTypeMid;
            
    public static MediaManager getManager()
    {
        synchronized(lock)
        {
            if (manager == null)
            {
                manager = new MediaManager();
            }
        }
        
        return manager;
    }
    
    private MediaManager()
    {
        player = null;
        currentMediaID = -1;
        is = null;
        
        mediaTypeWav = "audio/wav";
        mediaTypeMid = "audio/midi";
        
        String[] contents = Manager.getSupportedContentTypes(null);
        for (int i = 0; i < contents.length; i++)
        {
            if (contents[i].startsWith("audio"))
            {
                if (contents[i].indexOf("wav") > 0)
                {
                    mediaTypeWav = contents[i];
                }
                if (contents[i].indexOf("mid") > 0)
                {
                    mediaTypeMid = contents[i];
                }
            }
        }
    }
    
    private String getMediaType(String fileName)
    {
        if (fileName.endsWith(TYPE_MID))
        {
            return mediaTypeMid;
        }
        else
        {
            return mediaTypeWav;
        }
    }
    
    public synchronized int getCurrentMediaID()
    {
        return currentMediaID;
    }
    
    public synchronized void playMusic(int id) {
        playMusic(id, 1);
    }
    
    public synchronized void playMusic(int id, int loopCount)
    {
        //Проверяем есть ли такой ИД
        if (id >= SOUNDS.length)
        {
            return;
        }
        
        //Останавливаем предыдущую музыку
        stopMusic();
        
        //Определяем имя файла
        String fileName = MEDIA_FOLDER + SOUNDS[id].getKey();
        
        try
        {
            is = getClass().getResourceAsStream(fileName);
            currentMediaID = id;
            player = Manager.createPlayer(is, getMediaType(fileName));
            player.realize();
            
            //Изменяем громкость
            VolumeControl vc = null;
            try
            {
                vc = (VolumeControl)player.getControl("VolumeControl");
            }catch(Exception e){}
            if(vc != null)
            {
                vc.setLevel(100);
            }

            player.setLoopCount(loopCount);
            player.start();
        }
        catch(Exception e)
        {
            //ToDo Kvest
            //delete this
            //TestForm.LOG += "music[" + fileName + "]=" + e.getMessage() + "\n";
        }
    }
    
    public synchronized void stopMusic()
    {
        currentMediaID = -1;
	if (player != null)
	{
            try
            {
                player.stop();
            }catch(Exception e){}
            try
            {
		player.close();
            }catch(Exception e){}
            player = null;
	}
        if (is != null)
	{
            try
            {
                is.close(); 
            }catch(Exception e){}
            is = null;
	}
    }
}
